using System;
using SBT;
using System.DHTML;
using System.Silverlight;
using System.Silverlight.Presentation;

namespace Scripts02
{
    public class ButtonListsMgr : SLManager
    {
        public ButtonListsMgr(DOMElement host)
            : base(host)
        { }

        public override void OnSilverlightLoaded(System.Silverlight.SilverlightControl slControl, object args)
        {
            ControlContent content = slControl.Content;
            Downloader downloader = (Downloader)slControl.CreateObject("downloader");
            downloader.AddEventListener(DownloaderEvent.Completed, (Sys.EventHandler)delegate
            {
                string radioDown = downloader.GetResponseText("radioDown.xaml");
                string radioUp = downloader.GetResponseText("radioUp.xaml");
                string radioEnter = downloader.GetResponseText("radioEnter.xaml");
                string radioLeave = downloader.GetResponseText("radioLeave.xaml");

                string checkDown = downloader.GetResponseText("checkDown.xaml");
                string checkUp = downloader.GetResponseText("checkUp.xaml");
                string checkEnter = downloader.GetResponseText("checkEnter.xaml");
                string checkLeave = downloader.GetResponseText("checkLeave.xaml");

                ArrayList radios = new ArrayList();
                for (int i = 0; i < 5; i++)
                {
                    ToggleButton radio = new ToggleButton();
                    radio.Plugin = slControl;
                    radio.Element = (UIElement)content.FindName("radio" + i.ToString());
                    radio.MouseDownTemplate = radioDown;
                    radio.MouseEnterTemplate = radioEnter;
                    radio.MouseLeaveTemplate = radioLeave;
                    radio.MouseUpTemplate = radioUp;
                    radio.Initialize();
                    ArrayList.Add(radios, radio);
                }
                Sys.EventHandler radioChangedHandler = OnRadioSelectionChanged;

                ToggleButtonList radioList = new ToggleButtonList();
                radioList.Behavior = ToggleButtonListBehavior.RadioButtonList;
                radioList.Plugin = slControl;
                radioList.Items = radios;
                radioList.add_selectionChanged(radioChangedHandler);
                radioList.Initialize();

                ArrayList checks = new ArrayList();
                for (int i = 0; i < 5; i++)
                {
                    ToggleButton check = new ToggleButton();
                    check.Plugin = slControl;
                    check.Element = (UIElement)content.FindName("Check" + i.ToString());
                    check.MouseDownTemplate = checkDown;
                    check.MouseEnterTemplate = checkEnter;
                    check.MouseLeaveTemplate = checkLeave;
                    check.MouseUpTemplate = checkUp;
                    check.Initialize();
                    ArrayList.Add(checks, check);
                }
                Sys.EventHandler checkChangedHandler = OnCheckSelectionChanged;
                ToggleButtonList checkList = new ToggleButtonList();
                checkList.Behavior = ToggleButtonListBehavior.CheckboxButtonList;
                checkList.Plugin = slControl;
                checkList.Items = checks;
                checkList.add_selectionChanged(checkChangedHandler);
                checkList.Initialize();
            });
            downloader.Open("GET", "Templates.zip");
            downloader.Send();
        }
        private void OnRadioSelectionChanged(object sender, Sys.EventArgs args)
        {
            TextBlock txtBlock = (TextBlock)Plugin.Content.FindName("txtSelectedRadio");
            ToggleButtonList list = (ToggleButtonList)sender;
            FillSelectionTxtBlock(txtBlock, list);
        }
        private void OnCheckSelectionChanged(object sender, Sys.EventArgs args)
        {
            TextBlock txtBlock = (TextBlock)Plugin.Content.FindName("txtSelectedCheck");
            ToggleButtonList list = (ToggleButtonList)sender;
            FillSelectionTxtBlock(txtBlock, list);
        }

        private static void FillSelectionTxtBlock(TextBlock txtBlock, ToggleButtonList list)
        {
            string selectionText = "Selection: ";
            for (int i = 0; i < list.SelectedItems.Length; i++)
            {
                if (i > 0)
                    selectionText += ", ";
                selectionText += ((AgBehavior)list.SelectedItems[i]).ElementName;
            }
            txtBlock.Text = selectionText;
        }
    }
}
